package com.som.service.excel.data2.process;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;

import com.som.bean.data2.input.CompDataInput;
import com.som.bean.data2.input.MergerCompDataInput;
import com.som.bean.data2.output.AllSystemDifferentData;
import com.som.console.ExcelProcException;
import com.som.service.excel.ExcelConvOutputDataProcessInf;

/**
 * 将数据进行合并后的结果,输出不一样的数据
 * 
 * @author liujun
 * @date 2016年4月10日
 * @verion 0.0.1
 */
public class AllSystemDifferentOutProcess implements ExcelConvOutputDataProcessInf<Map<String, MergerCompDataInput>, AllSystemDifferentData>
{

	public Map<String, AllSystemDifferentData> convOutProcessData(Map<String, MergerCompDataInput> param, LinkedList<String> column) throws ExcelProcException
	{
		Map<String, AllSystemDifferentData> result = null;

		if (null != param && !param.isEmpty())
		{
			result = new LinkedHashMap<String, AllSystemDifferentData>();

			// 取得到的结果的迭代器对象
			Iterator<Entry<String, MergerCompDataInput>> IterMerger = param.entrySet().iterator();

			Entry<String, MergerCompDataInput> margerEntry = null;

			MergerCompDataInput mergerData = null;

			LinkedHashMap<String, CompDataInput> compDataMap = null;

			// 组装输出对象
			AllSystemDifferentData differentData = null;
			LinkedList<String> differentName = null;

			while (IterMerger.hasNext())
			{
				margerEntry = IterMerger.next();

				if (null != margerEntry.getValue())
				{
					mergerData = margerEntry.getValue();

					compDataMap = mergerData.getMargeMap();

					// 标识数据有不同的为false
					boolean isdifferent = false;
					// 1,检查所有数据都不为空
					if (compDataMap != null && !compDataMap.isEmpty())
					{
						// 首先比对的值是否与文件数量一至，只有一致时才进行比对
						// 如果列数量不相等，直接判定为不相等
						if (compDataMap.size() == column.size())
						{
							CompDataInput tmp1 = null;

							for (Entry<String, CompDataInput> compDataItem : compDataMap.entrySet())
							{
								if (null == tmp1)
								{
									tmp1 = compDataItem.getValue();
								} else
								{
									if (null != tmp1 && null != compDataItem.getValue() && compDataItem.getValue().getUserName().equals(tmp1.getUserName()))
									{
										isdifferent = true;
									} else
									{
										isdifferent = false;
										break;
									}
								}

							}
						}

						// 将有不同数据的信息给移除
						if (isdifferent == false)
						{
							IterMerger.remove();
							// 组将输出对象
							differentData = new AllSystemDifferentData();

							differentData.setIpaddress(mergerData.getIpaddress());

							differentName = new LinkedList<String>();

							if (null != column)
							{
								CompDataInput compDataOut = null;

								for (String col : column)
								{
									compDataOut = compDataMap.get(col);

									if (null != compDataOut)
									{
										differentName.add(compDataOut.getUserName());
									} else
									{
										differentName.add(null);
									}
								}
							}

							differentData.setSystemName(differentName);
							result.put(differentData.getIpaddress(), differentData);
						}

					}

				}
			}

		}

		return result;
	}

}
